<?php
/**
*	@package UHU_Components
*	@subpackage Smarty
**/

uhu::load_libs('Smarty');

/**
*	@package Smarty
**/
class uhu_smarty_exception extends uhu_exception
{
	protected function getExtendedMessage($code=null)
	{
		$message = '%s';
		switch($code)
		{
			case 100:
			case 101:
				$message = 'Error getting Smarty object: %s';
			break;
		}
		return $message;
	}
}

/**
*	@package Smarty
**/
abstract class Smarty_Conf
{
	abstract public function template_dir();
	abstract public function compile_dir();
	abstract public function cache_dir();
	protected $Smarty;
	public function __toString()
	{
		return md5($this->template_dir() . $this->compile_dir() . $this->cache_dir());
	}
	public function mkdir_mode()
	{
		return 0750;
	}
	public function mkdir_recursive()
	{
		return true;
	}
	public function make_dirs()
	{
		return false;
	}
	public function check_dirs()
	{
		if(is_dir($this->template_dir()) === false)
		{
			throw new uhu_smarty_exception('Template directory does not exist',100);
		}
		if($this->compile_dir() !== null)
		{
			if(is_string($this->compile_dir()) === false)
			{
				throw new uhu_smarty_exception('Invalid type specified for compile directory. String expected, ' . gettype($this->compile_dir()) . ' given',101);
			}
			else if(is_dir($this->compile_dir()) === false && $this->make_dirs() && @mkdir($this->compile_dir(),$this->mkdir_mode(),$this->mkdir_recursive()) === false)
			{
				throw new uhu_smarty_exception('Could not create compile directory',101);
			}
			else if(is_dir($this->compile_dir()) === false && $this->make_dirs() === false)
			{
				throw new uhu_smarty_exception('Compile directory does not exist',101);
			}
		}
		if($this->cache_dir() !== null)
		{
			if(is_string($this->cache_dir()) === false)
			{
				throw new uhu_smarty_exception('Invalid type specified for compile directory. String expected, ' . gettype($this->cache_dir()) . ' given',102);
			}
			else if(is_dir($this->cache_dir()) === false && $this->make_dirs() && @mkdir($this->cache_dir(),$this->mkdir_mode(),$this->mkdir_recursive()) === false)
			{
				throw new uhu_smarty_exception('Could not create compile directory',102);
			}
			else if(is_dir($this->cache_dir()) === false && $this->make_dirs() === false)
			{
				throw new uhu_smarty_exception('Compile directory does not exist',102);
			}
		}
	}
}

/**
*	@package Smarty
**/
class Smarty_Registry
{
	private static $objs = array();
	private static $cache_key;
	private static $mkdir_mode = 0750;
	private static $mkdir_recursive = true;
	final static function &get(Smarty_Conf $conf)
	{
		if(array_key_exists((string)$conf,self::$objs) === false)
		{
			self::add($conf);
		}
		return self::$objs[(string)$conf];
	}
	private static function add(Smarty_Conf $conf)
	{
		$_SERVER['SCRIPT_NAME'] = empty($_SERVER['SCRIPT_NAME']) ? null : $_SERVER['SCRIPT_NAME'];
		$GLOBALS['HTTP_SERVER_VARS']['SCRIPT_NAME'] = $_SERVER['SCRIPT_NAME'];
		$conf->check_dirs();
		$obj = new Smarty;
		$obj->template_dir = $conf->template_dir();
		$obj->compile_dir  = $conf->compile_dir();
		$obj->cache_dir    = $conf->cache_dir();
		self::$objs[(string)$conf] = $obj;
	}
}
?>